#include <iostream>
#include "OutImage.h"


COutImage::COutImage(void)
{
}

COutImage::COutImage(int _w, int _h)
{
	m_Width = _w;
	m_Height = _h;

	m_Data = new uint*[m_Width];

	for (int i = 0; i < m_Width; i++)
	{
		m_Data[i] = new uint[m_Height];

		for (int j = 0; j < m_Height; j++)
			m_Data[i][j] = 0x00000000;		
	}
}

COutImage::COutImage(int _w, int _h, uint** _data)
{
	m_Width = _w;
	m_Height = _h;

	m_Data = new uint*[m_Width];

	for (int i = 0; i < m_Width; i++)
	{
		m_Data[i] = new uint[m_Height];

		for (int j = 0; j < m_Height; j++)
			m_Data[i][j] = _data[i][j];		
	}
}

COutImage::~COutImage(void)
{
	delete []m_Data;
}

void COutImage::SetPixel(int _x, int _y, CLightIntensity &_pix)
{
	int r = (int) (_pix.GetRed() * 255);
	int g = (int) (_pix.GetGreen() * 255);
	int b = (int) (_pix.GetBlue() * 255);
	int a = (int) (_pix.GetAlpha() * 255);
	uint col = b;
	col |= g << 8;
	col |= r << 16;
	col |= a << 24;
	
	if ((_x >= 0 && _x < m_Width) && (_y >= 0 && _y < m_Height))
		m_Data[_x][_y] = col;
}

void COutImage::SetPixels(int _startX, int _startY, int _width, int _height, CLightIntensity& _pix)
{
	if (_width < 1)
		_width = 1;

	if (_height < 1)
		_height = 1;

	if ( (_startX + _width) > m_Width)
		_width += m_Width - _width;

	if ( (_startY + _height) > m_Height)
		_height += m_Height - _height;

	for (int i = _startX; i < _width; i++)
		for (int j = _startY; j < _height; j++)
			SetPixel(i, j, _pix);
}